package edu.upc.fib.lamsandroid.client.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import edu.upc.fib.lamsandroid.client.LamsAndroidClientApplication;
import edu.upc.fib.lamsandroid.client.R;
import edu.upc.fib.lamsandroid.client.dialog.ConfirmBoxDialog;
import edu.upc.fib.lamsandroid.client.dialog.HelpDialog;

public abstract class AbstractBaseActivity extends Activity {

    private boolean menuVisibilitySet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if(this instanceof MainActivity) {
            MenuItem item = menu.findItem(R.id.logout);
            item.setVisible(false);

            item = menu.findItem(R.id.settings);
            item.setVisible(true);

            item = menu.findItem(R.id.help);
            item.setVisible(true);

            item = menu.findItem(R.id.stop);
            item.setVisible(false);
        }

        if(this instanceof LessonListActivity) {
            MenuItem item = menu.findItem(R.id.logout);
            item.setVisible(true);

            item = menu.findItem(R.id.settings);
            item.setVisible(true);

            item = menu.findItem(R.id.help);
            item.setVisible(true);

            item = menu.findItem(R.id.stop);
            item.setVisible(false);
        }

        if(this instanceof LessonActivity) {
            MenuItem item = menu.findItem(R.id.logout);
            item.setVisible(false);

            item = menu.findItem(R.id.settings);
            item.setVisible(false);

            item = menu.findItem(R.id.help);
            item.setVisible(true);

            item = menu.findItem(R.id.stop);
            item.setVisible(true);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.logout:
                new ConfirmBoxDialog(this, new ConfirmBoxDialog.ConfirmBoxDialogListener() {
                    @Override
                    public void onAccept() {
                        LamsAndroidClientApplication.getInstance().setLogoutRequested(true);
                        startActivity(new Intent(getApplicationContext(), MainActivity.class));
                        finish();
                    }

                    @Override
                    public void onDeny() {
                        /*noop*/
                    }
                }).show("Are you sure you want to log out?");
                break;
            case R.id.settings:
                startActivity(new Intent(this, ConfigurationActivity.class));
                break;
            case R.id.help:
                HelpDialog hd = new HelpDialog(this);
                hd.show(resolveTitle(), resolveBody());
                break;
            case R.id.stop:
                startActivity(new Intent(getApplicationContext(), LessonListActivity.class));
                finish();
                break;

            default:
                return super.onOptionsItemSelected(item);
        }

        return true;
    }

    @Override
    public void onBackPressed() {
        //nothing
    }

    private String resolveTitle() {
        if(this instanceof MainActivity) {
            return getString(R.string.contextualHelpMainTitle);
        }

        return "** no contextual help title for this activity**";
    }

    private String resolveBody() {
        if(this instanceof MainActivity) {
            return getString(R.string.contextualHelpMainBody);
        }

        return "** no contextual help body for this activity**";
    }
}
